package com.tjetc.dao.impl;

import com.tjetc.dao.PayTypeDao;
import com.tjetc.pojo.dto.PayDTO;
import com.tjetc.pojo.vo.PayTypeVO;
import com.tjetc.utils.DruidUtils;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/17 18:56
 * @Description: PayTypeDaoImpl
 */
public class PayTypeDaoImpl implements PayTypeDao {
    @Override
    public PayTypeVO getByName(String payName) throws Exception {
        return DruidUtils.r2.query("select * from payment where name = ?", new BeanHandler<>(PayTypeVO.class), payName);
    }

    @Override
    public void insert(String payName) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "insert into payment (name) values (?)", payName);
    }

    @Override
    public void delete(String payName) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "delete from payment where name = ?", payName);
    }

    @Override
    public void update(PayDTO payDTO) throws Exception{
        DruidUtils.r1.update(DruidUtils.getConnection(), "update payment set name = ? where name = ?", payDTO.getNewPayName(), payDTO.getOldPayName());
    }

    @Override
    public List<PayTypeVO> list() throws Exception{
        return DruidUtils.r2.query("select * from payment order by id",new BeanListHandler<>(PayTypeVO.class));
    }
}
